package com.imay.live.fragment;


import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imay.base.fragment.BaseFragment;
import com.imay.live.activity.PermissionHelpActivity;
import com.imay.live.model.PermissionModel;
import com.imay.live.view.custom.LivePermissionView;
import com.imay.live.R;

/**
 * create by nick
 * 查看特权
 */
public class PermissionCheckFragment extends BaseFragment  {


    private static String ARG_PARAM1 = "param1";
    private static String ARG_PARAM2 = "param2";
    public static final int TYPE_USER=1;
    public static final int TYPE_LIVE=2;
    private RelativeLayout rl_top_permission;
    private RelativeLayout rl_level;
    private TextView tv_level_permission;
    private ProgressBar pb_expect_permission;
    private TextView tv_expect_permission;
    private ImageView iv_level_permission;
    private TextView tv_permission;
    private LinearLayout ll_user_permission;
    private LinearLayout ll_live_container_permission;
    private RadioButton rb_danmu_permission;
    private RadioButton rb_jinchang_permission;
    private RadioButton rb_zuojia_permission;
    private PermissionModel permissionModel;

    public static PermissionCheckFragment newInstance(PermissionModel model, int type) {
        PermissionCheckFragment fragment = new PermissionCheckFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, type);
        args.putSerializable(ARG_PARAM2,  model);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_permission_check;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        if (getArguments()!=null){
            permissionModel = (PermissionModel) getArguments().getSerializable(ARG_PARAM2);
        }
        bindViews();
        refreshView();
    }


    private boolean isUserTab(){
        return TYPE_USER==getArguments().getInt(ARG_PARAM1);
    }

    private void bindViews() {
        rl_top_permission = $(R.id.rl_top_permission);
        rl_level = $(R.id.rl_level);
        tv_level_permission = $(R.id.tv_level_permission);
        pb_expect_permission = $(R.id.pb_experience_permission);
        tv_expect_permission = $(R.id.tv_experience_permission);
        iv_level_permission = $(R.id.iv_level_permission);
        tv_permission = $(R.id.tv_permission);
        ll_user_permission = $(R.id.ll_user_permission);
        rb_danmu_permission = $(R.id.rb_danmu_permission);
        rb_jinchang_permission = $(R.id.rb_jinchang_permission);
        rb_zuojia_permission = $(R.id.rb_zuojia_permission);
        ll_live_container_permission = $(R.id.ll_live_container_permission);

        $(R.id.iv_help_permission).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PermissionHelpActivity.start(getActivity());
            }
        });
    }

    private void refreshView(){
        if (isUserTab()){
            tv_level_permission.setText("LV "+permissionModel.getUserLevel());
            rb_danmu_permission.setChecked(permissionModel.getUserLevel()>=10);
            rb_jinchang_permission.setChecked(permissionModel.getUserLevel()>=15);
            rb_zuojia_permission.setChecked(permissionModel.getUserLevel()>=25);
            tv_expect_permission.setText(String.format(getString(R.string.permission_experience),getCurrentExp(permissionModel.getUserExp(),permissionModel.getUserExpUpgradeNeed()),permissionModel.getUserExpUpgradeNeed()));
            pb_expect_permission.setMax(permissionModel.getUserExpUpgradeNeed());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                pb_expect_permission.setProgress(permissionModel.getUserExp(),true);
            }else{
                pb_expect_permission.setProgress(permissionModel.getUserExp());

            }
        }else{
            tv_level_permission.setText("LV "+permissionModel.getLiveLevel());
             ll_user_permission.setVisibility(View.GONE);
            tv_expect_permission.setText(String.format(getString(R.string.permission_experience),getCurrentExp(permissionModel.getLiveExp(),permissionModel.getLiveExpUpgradeNeed()),permissionModel.getLiveExpUpgradeNeed()));
            pb_expect_permission.setMax(permissionModel.getLiveExpUpgradeNeed());
            rl_level.setBackgroundResource(R.mipmap.ico_05qx_zhubo_pnl);
            tv_level_permission.setBackgroundResource(R.mipmap.ico_05qx_zhubo_lv_pnl);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                pb_expect_permission.setProgress(permissionModel.getLiveExp(),true);
            }else{
                pb_expect_permission.setProgress(permissionModel.getLiveExp());
            }
            ll_live_container_permission.addView(new LivePermissionView(permissionModel.getLiveLevel(),LivePermissionView.TYPE_LIVE,getActivity()));
            ll_live_container_permission.addView(new LivePermissionView(permissionModel.getLiveLevel(),LivePermissionView.TYPE_WEEK,getActivity()));
            ll_live_container_permission.addView(new LivePermissionView(permissionModel.getLiveLevel(),LivePermissionView.TYPE_MONEY,getActivity()));

        }

    }

    private int getCurrentExp(int currentExp , int maxExp){
        return currentExp>maxExp?maxExp:currentExp;
    }


}
